<?php

namespace Desktopd\MUA;


// Debuggable Actor
abstract class DebuggableActor implements Debuggable {
    /* Debuggable */
    protected $debugHandlers = array();
    
    public function addDebugListener (/*callable*/ $callback) {
        if (!is_callable($callback)) {
            return false;
        }
        
        if (!in_array($callback, $this->debugHandlers, true)) {
            $this->debugHandlers[] = $callback;
        }
        
        return true;
    }
    
    public function removeDebugListener (/*callable*/ $callback) {
        foreach (array_keys($this->debugHandlers, $callback, true) as $key) {
            unset($this->debugHandlers[$key]);
        }
        
        $this->debugHandlers = array_values($this->debugHandlers);
        
        return true;
    }
    
    /* package */
    public function callDebugger ($data = '') {
        $count = 0;
        foreach ($this->debugHandlers as $callback) {
            if (!is_callable($callback)) continue;
            $callback("$data");
            $count++;
        }
        
        return $count;
    }
    
}


// vim: ts=4 et ai

